package com.sh.dc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sh.dc.common.enums.TaskStatusEnum;
import com.sh.dc.common.pojo.bo.TaskStopReq;
import com.sh.dc.common.pojo.dto.TaskSolverDto;
import com.sh.dc.common.pojo.dto.TaskStatusCountDto;
import com.sh.dc.common.pojo.query.TaskSolverPageQuery;
import com.sh.dc.config.SolverSchedulerConfig;
import com.sh.dc.entity.Solver;
import com.sh.dc.entity.TaskSolver;
import com.sh.dc.mapper.TaskSolverMapper;
import com.sh.dc.service.ITaskSolverService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 求解任务表 服务实现类
 * </p>
 *
 * @author zhenlan
 * @since 2024-09-19
 */
@Slf4j
@Service
public class TaskSolverServiceImpl extends ServiceImpl<TaskSolverMapper, TaskSolver> implements ITaskSolverService {

    @Autowired
    private TaskSolverMapper taskSolverMapper;

    @Autowired
    private SolverSchedulerConfig solverSchedulerConfig;

    @Override
    public IPage<TaskSolverDto> getTaskSolverPage(TaskSolverPageQuery query) {
        Page<TaskSolver> page = new Page<>(query.getPageNum(), query.getPageSize());
        page.addOrder(OrderItem.desc("id"));
        QueryWrapper<TaskSolver> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(query.getName())) {
            queryWrapper.like("name", query.getName());
        }
        if (StringUtils.isNotBlank(query.getStatus())) {
            queryWrapper.eq("status", query.getStatus());
        }
        if (query.getSolverId() != null) {
            queryWrapper.eq("solver_id", query.getSolverId());
        }
        if (query.getStartTime() != null && query.getEndTime() != null) {
            queryWrapper.between("create_time", query.getStartTime(), query.getEndTime());
        }
        if (StringUtils.isNotBlank(query.getCreateBy())) {
            queryWrapper.like("create_by", query.getCreateBy());
        }
        IPage<TaskSolver> result = taskSolverMapper.selectPage(page, queryWrapper);
        return result.convert(this::transferToDto);
    }

    @Override
    public TaskSolverDto transferToDto(TaskSolver solver) {
        if (solver == null) {
            return null;
        }
        TaskSolverDto taskSolverDto = new TaskSolverDto();
        BeanUtil.copyProperties(solver, taskSolverDto);
        if (StringUtils.isNotBlank(solver.getStatus())) {
            taskSolverDto.setStatusName(TaskStatusEnum.getNameByCode(solver.getStatus()));
        }
        return taskSolverDto;
    }

    @Override
    public List<TaskStatusCountDto> countStatus() {
        return taskSolverMapper.countStatus();
    }

    @Override
    public void stopTask(Integer taskId) {
        String rep = HttpUtil.post(solverSchedulerConfig.getTaskStopUrl(), JSON.toJSONString(TaskStopReq.builder().id(taskId.toString()).build()));
        log.info("stop task {}, rep: {}", taskId, rep);
    }
}
